<template>
  <el-menu
    :default-active="activeIndex"
    class="el-menu-demo"
    mode="horizontal"
    @select="handleSelect"
  >
  <img src="../assets/logo.png" alt="" class="logo">
    <el-menu-item index="1" @click="toIndex">要闻</el-menu-item>
    <el-menu-item index="2" @click="toCurrent">时事</el-menu-item>
    <el-menu-item index="3" @click="toTec">科技</el-menu-item>
    <el-menu-item index="4" @click="toFinance">财经</el-menu-item>
    <el-menu-item index="5" @click="toInter">国际</el-menu-item>
    <el-input type="text" placeholder="请输入内容" class="search" v-model="keyword" @keydown.enter="search" clearable>
      <template #append><el-button @click="search"><el-icon class="icon"><Search /></el-icon></el-button></template>
    </el-input>
    <div>
      <el-button type="text" class="login-box" v-if="user===null" @click="toLogin">登陆</el-button>
      <div v-else>
        <el-avatar :size="40" :src="require('../assets/avatar/'+userId+'.jpg')" class="tou" @click="toIndividual"></el-avatar>
        <div class="shu">|</div>
        <el-popconfirm
    width="220"
    confirm-button-text="确认"
    cancel-button-text="取消"
    title="您确认要退出登陆吗？"
    @confirm="toLogin"
    >
    <template #reference>
      <el-button type="text" class="login-box">注销</el-button>
    </template>
  </el-popconfirm>
      </div>
    </div>
  </el-menu>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search } from '@element-plus/icons'
export default defineComponent({
  name: 'NavBarView',
  setup () {
    const activeIndex:any = ref('1')
    const router = useRouter()
    const keyword = ref('')
    const user = sessionStorage.getItem('user')
    const userId = sessionStorage.getItem('userId')
    const setNewsTag = () => {
      sessionStorage.setItem('newsTag', '要闻')
    }
    setNewsTag()
    const toIndex = () => {
      sessionStorage.setItem('newsTag', '要闻')
      router.push('/')
    }
    const toTec = () => {
      sessionStorage.setItem('newsTag', '科技')
      router.push('/tec')
    }
    const toCurrent = () => {
      sessionStorage.setItem('newsTag', '时事')
      router.push('/current')
    }
    const toFinance = () => {
      sessionStorage.setItem('newsTag', '财经')
      router.push('/finance')
    }
    const toInter = () => {
      sessionStorage.setItem('newsTag', '国际')
      router.push('/international')
    }
    const toIndividual = () => {
      router.push('/individual')
    }
    const toLogin = () => {
      sessionStorage.clear()
      router.push('login')
    }
    const handleSelect = (key: string, keyPath: string[]) => {
      console.log(key, keyPath)
    }
    const search = () => {
      if (keyword.value === '') {
        ElMessage.warning('请输入搜索内容')
      } else {
        router.push({ name: 'result', query: { keyword: keyword.value } })
        activeIndex.value = ref('')
        keyword.value = ''
      }
    }
    return {
      search,
      handleSelect,
      toLogin,
      toIndividual,
      toInter,
      toCurrent,
      toFinance,
      toTec,
      toIndex,
      userId,
      user,
      activeIndex,
      keyword,
      Search
    }
  },
  components: { Search }
})
</script>
<style scoped>
.el-menu-demo{
  z-index:11;
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
}
.search{
  width: 250px;
  height: 30px;
  left: 180px;
  top: 15px;
}
.icon{
  color: #409EFF;
}
.login-box{
  float: right;
  margin-left: 448px;
  top: 20px;
  height: 10px;
  position: absolute;
}
.tou{
  position: absolute;
  top: 10px;
  right: 110px;
  cursor: pointer;
}
.shu{
  position: absolute;
  top: 18px;
  right: 90px;
}
.logo{
  height: 50px;
  width: 150px;
}
</style>
<style>
a:-webkit-any-link {
  color: #409EFF;
  font-size: small;
}
</style>
